//
//  CAGradientLayer+ABCDynamic.m
//  ABCAppearanceKit_Example
//
//  Created by mac on 2022/9/9.
//  Copyright © 2022 Joe. All rights reserved.
//

#import "CAGradientLayer+ABCDynamic.h"
#import "NSObject+ABCDynamic.h"
#import <objc/runtime.h>

@implementation CAGradientLayer (ABCDynamic)

- (void)abc_dynamicWithLightColors:(NSArray<UIColor *> *)lightColors darkColors:(NSArray<UIColor *> *)darkColors {
    if (@available(iOS 13.0, *)) {
        self.lightColors = lightColors;
        self.darkColors = darkColors;
        [self abc_bindDynamicUpdate:^(ABCAppearanceDynamicStyle dynamicStyle) {
            NSMutableArray *colors = [[NSMutableArray alloc]init];
            for (UIColor *color in (dynamicStyle == ABCAppearanceDynamicStyle_Dark ? darkColors : lightColors)) {
                [colors addObject:(__bridge id)color.CGColor];
            }
            self.colors = colors;
        }];
    } else {
        NSMutableArray *colors = [[NSMutableArray alloc]init];
        for (UIColor *color in lightColors) {
            [colors addObject:(__bridge id)color.CGColor];
        }
        self.colors = colors;
    }
}

- (NSArray<UIColor *> *)lightColors {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setLightColors:(NSArray<UIColor *> *)lightColors {
    objc_setAssociatedObject(self, @selector(lightColors), lightColors, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSArray<UIColor *> *)darkColors {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setDarkColors:(NSArray<UIColor *> *)darkColors {
    objc_setAssociatedObject(self, @selector(darkColors), darkColors, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end
